
package com.wyzz.hr.bo.impl;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.criterion.*;

import com.wyzz.hr.bo.IOvertimetypeBo;
import com.wyzz.hr.dao.IOvertimetypeDAO;
import com.wyzz.hr.pojo.Overtimetype;

public class OvertimetypeBoImpl implements IOvertimetypeBo {

	public OvertimetypeBoImpl() {
	}

	public Overtimetype getOvertimetype(String inputOtNo) {
		String fetch[] = null;
		return (Overtimetype) dao.loadObject(
				com.wyzz.hr.pojo.Overtimetype.class, inputOtNo, fetch,
				new boolean[0]);
	}

	public boolean addOvertimetype(Overtimetype overtimetype) {
		String trimmedOvertimetypeName = overtimetype.getOtName().trim();
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Overtimetype.class);
		dc.add(Restrictions.eq(Overtimetype.PROP_OT_NAME,
				trimmedOvertimetypeName));
		List old = dao.findByCriteria(dc);
		if (old.isEmpty()) {
			overtimetype.setOtName(trimmedOvertimetypeName);
			overtimetype
					.setOtSortId(getMaxRecordNo("Overtimetype", "otSortId"));
			dao.saveObject(overtimetype);
			return true;
		}
		try {
			return false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public Integer getMaxRecordNo(String tableName, String columnName) {
		List maxNoList = dao.exeHqlList((new StringBuilder()).append(
				"select max(").append(columnName).append(") from ").append(
				tableName).toString());
		Integer maxID;
		if (maxNoList == null || maxNoList.size() < 1
				|| maxNoList.get(0) == null)
			maxID = Integer.valueOf(1);
		else
			maxID = Integer.valueOf(1 + ((Integer) (Integer) maxNoList.get(0))
					.intValue());
		return maxID;
	}

	public List FindAllOtType() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Overtimetype.class);
		dc.addOrder(Order.asc(Overtimetype.PROP_OT_SORT_ID));
		return dao.findByCriteria(dc);
	}

	public Overtimetype searchByID(String otNo) {
		dao.loadObject(com.wyzz.hr.pojo.Overtimetype.class, otNo, null,
				new boolean[0]);
		Object ob = dao.loadObject(com.wyzz.hr.pojo.Overtimetype.class, otNo,
				null, new boolean[0]);
		Overtimetype overtimetype;
		if (ob == null)
			overtimetype = null;
		else
			overtimetype = (Overtimetype) ob;
		return overtimetype;
	}

	public void saveOvertimetypeByBatch(String ids[]) {
		if (ids == null)
			return;
		int sort = 1;
		int len = ids.length;
		for (int i = 0; i < len; i++) {
			dao.exeHql((new StringBuilder()).append(
					"update Overtimetype set otSortId=").append(sort).append(
					" where id ='").append(ids[i]).append("'").toString());
			sort++;
		}

	}

	public boolean delOvertimetype(Class name, String id) {
		try {
			List check = new ArrayList();
			boolean isRefed = false;
			check = dao.exeHqlList((new StringBuilder()).append(
					"select count(*) from Overtimerequest where orOtNo = '")
					.append(id).append("'").toString());
			if (Integer.valueOf(check.get(0).toString()).intValue() > 0)
				isRefed = true;
			Overtimetype overtimetype = (Overtimetype) dao.loadObject(name, id,
					null, new boolean[0]);
			if (!isRefed && overtimetype != null) {
				dao.deleteObject(overtimetype);
				return true;
			}
		} catch (Exception e) {
			return false;
		}
		return false;
	}

	public boolean updateOvertimetype(Overtimetype overtimetype) {
		Overtimetype oldOvertimetype = (Overtimetype) dao.loadObject(
				com.wyzz.hr.pojo.Overtimetype.class, overtimetype.getOtNo(),
				null, new boolean[0]);
		if (oldOvertimetype != null) {
			String trimmedOvertimetypeName = overtimetype.getOtName().trim();
			oldOvertimetype.setOtName(trimmedOvertimetypeName);
			oldOvertimetype.setOtDesc(overtimetype.getOtDesc());
			oldOvertimetype.setOtOverLimit(overtimetype.getOtOverLimit());
			oldOvertimetype.setOtHourRate(overtimetype.getOtHourRate());
			dao.updateObject(oldOvertimetype);
			return true;
		} else {
			return false;
		}
	}

	public List findOvertimetypeList(Overtimetype overtimetype) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Overtimetype.class);
		dc.add(Restrictions.eq(Overtimetype.PROP_OT_NAME, overtimetype
				.getOtName().trim()));
		dc
				.add(Restrictions.ne(Overtimetype.PROP_OT_NO, overtimetype
						.getOtNo()));
		List t = dao.findByCriteria(dc);
		return t;
	}

	public List findByName(String name) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Overtimetype.class);
		dc.add(Restrictions.eq(Overtimetype.PROP_OT_NAME, name.trim()));
		List t = dao.findByCriteria(dc);
		return t;
	}

	public IOvertimetypeDAO getDao() {
		return dao;
	}

	public void setDao(IOvertimetypeDAO dao) {
		this.dao = dao;
	}

	private IOvertimetypeDAO dao;
}
